/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.CustomExpandCard;
import it.gmariotti.cardslib.demo.fragment.BaseMaterialFragment;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardListView;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

/**
 * List of expandable cards Example
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeListExpandCardFragment extends BaseMaterialFragment {

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_subtitle_lexpand;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group3;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_list_expand);
        initCards();
    }


    private void initCards() {

        //Init an array of Cards
        ArrayList<Card> cards = new ArrayList<Card>();
        for (int i = 0; i < 200; i++) {
            Card card = init_standard_header_with_expandcollapse_button_custom_area("Header " + i, i);
            cards.add(card);
        }

        CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this, cards);

        CardListView listView = (CardListView) this.findComponentById(ResourceTable.Id_carddemo_list_expand);
        if (listView != null) {
            listView.setAdapter(mCardArrayAdapter);
        }
    }


    /**
     * This method builds a standard header with a custom expand/collpase
     * @param titleHeader titleHeader
     * @param i position
     * @return Card
     */
    private Card init_standard_header_with_expandcollapse_button_custom_area(String titleHeader, int i) {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        header.setTitle(titleHeader);

        //Set visible the expand/collapse button
        header.setButtonExpandVisible(true);

        //Add Header to card
        card.addCardHeader(header);

        //This provides a simple (and useless) expand area
        CustomExpandCard expand = new CustomExpandCard(this, i);
        //Add Expand Area to Card
        card.addCardExpand(expand);

        //Just an example to expand a card
        if (i == 2 || i == 7 || i == 9)
            card.setExpanded(true);

        //Swipe
        card.setSwipeable(true);

        //Animator listener
        card.setOnExpandAnimatorEndListener(card1 -> new ToastDialog(getContext()).setText("Expand " + card1.getCardHeader().getTitle()).show());

        card.setOnCollapseAnimatorEndListener(card12 -> new ToastDialog(getContext()).setText("Collpase " + card12.getCardHeader().getTitle()).show());

        return card;
    }

}
